import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { IView } from "../../mvc/interfaces/IView";
import { GuildShopModule } from "../../../modules/GuildShopModule";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { DataEvent } from "../events/DataEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { SGuildShopUpdate } from "../../../Message/Game/SGuildShopUpdate";
import { GameProxy } from "../mvc/GameProxy";
import { Global } from "../../../com/gengine/global/Global";
import { GameController } from "../mvc/GameController";
import { IGuildShopModule } from "../../../modules/interfaces/IGuildShopModule";
type int = number;
//class GuildShopController
    
    export  class GuildShopController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.GuildOpenShop, this.onGuildOpenShopHandler);
            Dispatcher.addEventListener(EventName.GuildGetShopItems, this.onGuildGetShopItemsHandler);
            Dispatcher.addEventListener(EventName.GuildGetShopItemsSuccess, this.onGuildGetShopItemsSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildBuyShopItem, this.onGuildBuyShopItemHandler);
            return;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (null == this._shopModule) 
            {
                this._shopModule = new GuildShopModule();
                this._shopModule.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            return this._shopModule;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            NpcEffectRule.registDialogEffectWindow(this.view);
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetShopItems));
            this._shopModule.setSelectedBuyBtn();
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.GuildShopUpdate, this.onGuildShopUpdateHandler);
            return;
        }

        /* internal  */onGuildShopUpdateHandler(arg1: SGuildShopUpdate=null): void
        {
            var loc1=null;
            if (this._shopModule && arg1) 
            {
                loc1 = arg1.shopItems;
                if (arg1.reset != true) 
                {
                    this._shopModule.replaceShopItem(loc1[0]);
                }
                else 
                {
                    this._shopModule.setAllItems(loc1);
                }
            }
            return;
        }

        /* internal  */onGuildGetShopItemsHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.getGuildShopItems();
            return;
        }

        /* internal  */onGuildGetShopItemsSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            loc1 = cache.guild.getSortedGuildShopItems(loc1);
            this._shopModule.setAllItems(loc1);
            return;
        }

        /* internal  */onGuildBuyShopItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["itemCode"] as int;
            var loc2=arg1.data["amount"] as int;
            GameProxy.guildProxy.buyGuildShopItem(loc1, loc2);
            return;
        }

        /* internal  */onGuildOpenShopHandler(arg1: DataEvent): void
        {
            if (!cache.guild.myGuildInfo) 
            {
                return;
            }
            var loc1=0;
            if (arg1.data) 
            {
                loc1 = arg1.data as int;
            }
            var loc2=(Global.stage.stageHeight - GameController.pack.view.height) / 2;
            this.view.show(380, loc2);
            GameController.pack.view.show(710, loc2);
            this._shopModule.showShopByIndex(loc1);
            return;
        }

        private /* var */_shopModule: IGuildShopModule;
    }
